package com.attendance.controller;

import com.attendance.common.Result;
import com.attendance.dto.request.UserQueryRequest;
import com.attendance.dto.request.UserUpdateRequest;
import com.attendance.dto.request.UserRoleRequest;
import com.attendance.dto.request.UserPasswordUpdateRequest;
import com.attendance.entity.User;
import com.attendance.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
public class UserManageController {
    
    @Autowired
    private UserService userService;
    
    @GetMapping("/list")
    public Result<IPage<User>> queryUsers(UserQueryRequest request) {
        return Result.success(userService.queryUsers(request));
    }
    
    @PutMapping("/update")
    public Result<Void> updateUser(@Valid @RequestBody UserUpdateRequest request) {
        userService.updateUser(request);
        return Result.success();
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return Result.success();
    }
    
    @PutMapping("/{id}/enable")
    public Result<Void> enableUser(@PathVariable Long id) {
        userService.enableUser(id);
        return Result.success();
    }
    
    @PutMapping("/{id}/disable")
    public Result<Void> disableUser(@PathVariable Long id) {
        userService.disableUser(id);
        return Result.success();
    }

    @PutMapping("/role")
    @PreAuthorize("hasRole('ADMIN')")  // 只有管理员可以设置用户角色
    public Result<Void> setUserRole(@Valid @RequestBody UserRoleRequest request) {
        userService.setUserRole(request);
        return Result.success();
    }

    @PutMapping("/password")
    public Result<Void> updateUserPassword(@Valid @RequestBody UserPasswordUpdateRequest request) {
        userService.updateUserPassword(request);
        return Result.success();
    }
} 